--    Program: 		CAPPOIMP_JOB_TL.sql
--    Description:  Import PO JOB information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         27-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_job_id					number;
                    		                 		
   cursor cur_per_jobs_tl is
      select * from CRST_PER_JOBS_TL;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_per_jobs_tl loop 
   	 l_err_msg_tmp := '';
   	 
   	 l_job_id := null; 	
     begin
       SELECT job_id_n
       INTO   l_job_id
       FROM   CRST_PER_JOBS
       WHERE  job_id = cur.JOB_ID;
     exception
       when others then
         l_err_msg_tmp := 'New Job ID '||cur.JOB_ID||' not found';
     end;  	  
     
     begin
       -- import data
       insert into PER_JOBS_TL
       (
            JOB_ID             ,
            LANGUAGE           ,
            SOURCE_LANG        ,
            NAME               ,
            CREATED_BY         ,
            CREATION_DATE      ,
            LAST_UPDATED_BY    ,
            LAST_UPDATE_DATE   ,
            LAST_UPDATE_LOGIN  
       )
       values
       (
            l_job_id             ,
            cur.LANGUAGE           ,
            cur.SOURCE_LANG        ,
            cur.NAME               ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.CREATION_DATE      ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
            cur.LAST_UPDATE_DATE   ,
            cur.LAST_UPDATE_LOGIN  
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPERIMP_JOBS_TL', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
